<?php

!defined('IN_VIFRAME') && die('Access Denied.');

class Lang extends TLib {
	
	public static
		$Langs = array();
		
	protected static
		$_Loaded = array();

	public static function load($key, $default = array(), $filterAlias = VI_SYSTEM_FILTERALIAS) {
		$path = Vi::getImportFilesByPath("Lang : $key", $filterAlias);
		$path = $path[0];
		
		if (!self::isLoaded($path)) {
			if (is_file($path)) {
				$file = @include($path);
			} else {
				return false;
			}
			$_Loaded[$path] = true;
			
			if (false === $file) {
				$file = $default;
			}
	
			self::$Langs = $file + self::$Langs;
		}

		return true;
	}
	
	public static function isLoaded($path) {
		return (isset(self::$_Loaded[$path]) && self::$_Loaded[$path]);
	}

	public static function getLoadedFiles() {
		return array_keys(self::$_Loaded);
	}

	public static function get($key) {
		$subkeys = explode(':', $key);
		$config  = self::$Langs;
		foreach ($subkeys as $subkey) {
			if (isset($config[$subkey])) {
				$config = $config[$subkey];
			} else {
				return false;
			}
		}
		
		return $config;
	}
	
	public static function getAll() {
		return self::$Langs;
	}
	
	public static function set($key, $value = VI_LANG_RECURSIVESET) {
		if (is_array($key)) {
			if ($value) {
				foreach($key as $cntKey => $cntValue) {
					self::set($cntKey, $cntValue);
				}
			} else {
				self::$Langs = $key + self::$Langs;
			}
		} else {
			$subkeys = explode(':', $key);
			$config  = & self::$Langs;
			foreach ($subkeys as $subkey) {
				if (isset($config[$subkey])) {
					$config = & $config[$subkey];
				} else {
					$config[$subkey] = array();
					$config = & $config[$subkey];
				}
			}
			$config = $value;
		}
		
		return true;
	}
	
	public static function remove($key) {
		unset(self::$Langs[$key]);
			
		return true;
	}
	
	public static function removeAll() {
		self::$Langs = array();
		
		return true;
	}
}
